<#include "${ftl_skin}/common/header.ftl">
</head>
<body class="bg-none">
<div class="row wrapper animated fadeInRight">
    <div class="popup-box popup-jursdt col-sm-12">
        <div class="col-sm-10">
            <!--panel-options-->
            <div class="panel-options">
                <#--<h5>权限配置</h5>-->
                <ul class="nav nav-tabs">
                    <li class="active" data-treeid="treeview1">
                        <a href="project_detail.html#tab-1" data-toggle="tab" aria-expanded="true">菜单权限</a>
                    </li>
                    <li class="" data-treeid="treeview2">
                        <a href="project_detail.html#tab-2" data-toggle="tab" aria-expanded="false">功能权限</a>
                    </li>
                </ul>
            </div>
            <!--panel-options end-->
            <div class="panel blank-panel">
                <div class="tab-content">
                    <div class="tab-pane active" id="tab-1">
                        <div class="ibox-title">
                            <h5>[菜单列]</h5>
                            <div class="ibox-tools">
                                <em>禁用</em>
                                <em>启用</em>
                            </div>
                        </div>
                        <div id="treeview1" class="test" style="overflow-y: auto"></div>
                    </div>
                    <div class="tab-pane" id="tab-2">
                        <div class="ibox-title">
                            <h5>[功能列]</h5>
                            <div class="ibox-tools">
                                <em>禁用</em>
                                <em>启用</em>
                            </div>
                        </div>
                        <div id="treeview2" class="test" style="overflow-y: auto"></div>

                    </div>
                </div>
            </div>
        </div>
        <!--按钮-->
        <div class="col-sm-2 m-t-lg">
            <div class="m-t">
                <button type="button" class="btn btn-primary"
                        onClick="save();">保存</button>
            </div>
            <div class="m-t">
                <button type="button" class="btn btn-primary"
                        onClick="resetTree();">重置</button>
            </div>
            <div class="m-t">
                <button type="button" class="btn btn-primary open-all"
                >全部展开</button>
            </div>
            <div class="m-t">
                <button type="button" class="btn btn-primary close-all"
                >全部收起</button>
            </div>
        </div>
        <!--按钮-->
    </div>
</div>
<#include "${ftl_skin}/common/js.ftl">
<script src="${path.skinPath }js/demo/layer-demo.min.js"></script>
<script src="${path.skinPath }js/plugins/treeview/bootstrap-treeview.js"></script>
<script src="${path.skinPath }js/plugins/sweetalert/sweetalert.min.js"></script>
<script type="text/javascript">
    $(function () {

        $("#treeview1").height($(document.body).height() - 80);
        $("#treeview2").height($(document.body).height() - 80);

        $("#treeview1").treeview({
            data: getTreeData("${path.servicePath}auth/menutree.do", {"id": "${id}", "type": "${type!}"}),
            showTags: !0,
            showCheckboxs: !0
        });

        $("#treeview2").treeview({
            data: getTreeData("${path.servicePath}auth/functree.do", {"id": "${id}", "type": "${type!}"}),
            showTags: !0,
            showCheckboxs: !0
        });

        $(".open-all").click(function () {
            var treeid = $(".nav-tabs").find(".active").attr("data-treeid");
            if (treeid == "treeview1") {
                $("#treeview1").treeview({
                    data: getTreeData("${path.servicePath}auth/menutree.do", {"id": "${id}", "type": "${type!}"}),
                    levels: 99,
                    showTags: !0,
                    showCheckboxs: !0
                });
            } else if (treeid == "treeview2") {
                $("#treeview2").treeview({
                    data: getTreeData("${path.servicePath}auth/functree.do", {"id": "${id}", "type": "${type!}"}),
                    levels: 99,
                    showTags: !0,
                    showCheckboxs: !0
                });
            }
        });

        $(".close-all").click(function () {
            var treeid = $(".nav-tabs").find(".active").attr("data-treeid");
            if (treeid == "treeview1") {
                $("#treeview1").treeview({
                    data: getTreeData("${path.servicePath}auth/menutree.do", {"id": "${id}", "type": "${type!}"}),
                    levels: 1,
                    showTags: !0,
                    showCheckboxs: !0
                });
            } else if (treeid == "treeview2") {
                $("#treeview2").treeview({
                    data: getTreeData("${path.servicePath}auth/functree.do", {"id": "${id}", "type": "${type!}"}),
                    levels: 1,
                    showTags: !0,
                    showCheckboxs: !0
                });
            }
        });

    });

    function getTreeData(url, params) {
        var treeList, treeData;

        $.ajax({
            url: url,
            async: false,
            type: 'POST',
            data: params,
            complete: function (XMLHttpRequest, textStatus) {
            },
            success: function (data) {
                if (data.state == 0) {
                    treeList = data.result;
                }
            }
        });

        if (treeList.length > 0) {
            for (var i = 0; i < treeList.length; i++) {
                if (i < 0) {
                    continue;
                } else {
                    //treeList[i].checkboxs= [,{"name":"use","checked":"true"}];
                    var isChecked = treeList[i].is_auth == '1';
                    var isDisabled = treeList[i].is_disabled == '1';
                    treeList[i].checkboxs = [{"name": "use", "checked": isChecked}, {
                        "name": "forbidden",
                        "checked": isDisabled
                    }];
                }
            }

            treeData = toTree(treeList, "-1");
        }

        return treeData;
    }

    function tree_botton_bind() {
    }


    function getData(viewid) {
        var array = [];

        var treeview = $("#" + viewid).data("plugin_treeview");

        var tree_list = [];

        $.each(treeview.tree, function (index, nodes) {
            treeview._jsonTreeToList(nodes, tree_list);
        });

        var nodeIdArray = [];
        $.each(tree_list, function (nodeid, node) {
            if ($.inArray(node.id, nodeIdArray)) {
                nodeIdArray.push(node.id);
                var checkChanged = 0;
                //var node = $(this).data("node-info");
                var ischecked = 0;
                var isdisabled = 0;

                $.each(node.checkboxs, function (i, checkboxObj) {

                    if (checkboxObj.changed && (checkboxObj.changed == true || checkboxObj.changed == 'true')) {
                        checkChanged = 1;
                    }

                    if (checkboxObj.name == "use" && checkboxObj.checked == true) {
                        ischecked = 1;
                    }

                    if (checkboxObj.name == "forbidden" && checkboxObj.checked == true) {
                        isdisabled = 1;
                    }
                });
                if (checkChanged == 1) {
                    var dataObj = {
                        "edit": node.is_edit,
                        "node_seq": node.id,
                        "node_seq_code": node.tree_code,
                        // "tree_type": node.tree_type,
                        "is_auth": ischecked,
                        "is_disabled": isdisabled
                    };
                    array.push(dataObj);
                }


            }
        });
        return array;
    }

    function save() {
        // console.log(getData("treeview2"));
        // return false;
        $.ajax({
            url: "${path.servicePath}auth/authsave.do",
            async: false,
            type: 'POST',
            contentType: "application/json",
            data: JSON.stringify({
                id: '${id!}',
                type: '${type!}',
                menu: getData("treeview1"),
                func: getData("treeview2")
            }),
            complete: function (XMLHttpRequest, textStatus) {
            },
            success: function (data) {
                winReload();
            }
        });
    }


    function resetTree() {
        var treeid = $(".nav-tabs").find(".active").attr("data-treeid");
        if (treeid == "treeview1") {
            $("#treeview1").treeview({
                data: getTreeData("${path.servicePath}auth/menutree.do", {"id": "${id}", "type": "${type!}"}),
                showTags: !0,
                showCheckboxs: !0
            });
        } else if (treeid == "treeview2") {
            $("#treeview2").treeview({
                data: getTreeData("${path.servicePath}auth/functree.do", {"id": "${id}", "type": "${type!}"}),
                showTags: !0,
                showCheckboxs: !0
            });
        }
    }

    function toTree(data, pid) {
        var result = [], temp;
        for (var i = 0; i < data.length; i++) {
            if (data[i].parent_tree_code == pid) {
                var obj = data[i];
                obj.id = data[i].id;
                obj.tree_code = data[i].tree_code;
                obj.text = data[i].node_name;
                temp = toTree(data, data[i].tree_code);
                if (temp.length > 0) {
                    obj.nodes = temp;
                }
                result.push(obj);
            }
        }
        return result;
    }

</script>
<#include "${ftl_skin}/common/footer.ftl">
